import java.io.File;
import java.io.FilenameFilter;
import java.util.List;

public class RecoverWorkRoutine implements Runnable {

	// Callback implementation
	Callback c;
	private int timer;

	/* Constructor */
	public RecoverWorkRoutine(Callback c) {

		this.c = c;
	}

	/* Main routine */
	public void run() {

		// DEBUG
		/*System.out.println("Working directory : ");
		System.out.println(PrefProperties.workingDirectory + "/.received");*/
		
		// Initiate the working dir and the filter
		File dir = new File(PrefProperties.workingDirectory + "/.received");
		if(!dir.exists())
			dir.mkdir();
		FilenameFilter extension = FileOperation.withExtension("crp");
		// List all dir
		List<File> allFiles = FileOperation.listDir(dir, extension);

		// DEBUG
		System.out.println("Files founded : ");
		for (File file : allFiles) {		
			System.out.println(file);
		}
		
		// For all the folder in the directory
		for (File fileToRecover : allFiles) {
			
			File toDecryptFile = new File(fileToRecover.getAbsolutePath());
			File toUnserializeFile = FileOperation.decryptFile(toDecryptFile,
					PrefProperties.password);
			FileOperation.unserializeFile(toUnserializeFile);
			
			fileToRecover.delete();
		}
				
		this.c.callback();// callback
	}

}
